package com.nerve.secondhandcar.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.ko.core.dao.support.Finder;
import org.ko.core.dao.support.Updater;
import org.ko.core.websupport.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.nerve.secondhandcar.controller.model.EvaluatorsModel;
import com.nerve.secondhandcar.dao.EvaluatorsDao;
import com.nerve.secondhandcar.domain.Evaluators;
import com.nerve.secondhandcar.service.EvaluatorsService;

/**
 * 平台中的测试师对象Service接口实现
 * @author 集成显卡
 * @version 2014-10-14 15:29:15
 */
@Service
@Transactional
public class EvaluatorsServiceImpl implements EvaluatorsService {

	@Autowired
	private EvaluatorsDao dao;

	@Override
	public Evaluators addDo(Evaluators bean) {
		dao.save(bean);

		return bean;
	}
	
	public void create(EvaluatorsModel model)throws Exception{
		Evaluators entity = (model.getBean());
		
		if(entity != null){
			//判断测评师的名字
			Assert.hasText(entity.getName(), "测评师的名字不能为空");
			if(entity.getId() != null && entity.getId() >0){
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式	
				String date=df.format(new Date());
				Date d=df.parse(date);
				entity.setPostDate(d);
				dao.merge(entity);
			}else{
				entity.setPostDate(new Date());
				dao.save(entity);
			}
		}
	}
	/**
	 * 删除实体
	 * @param model
	 * @throws Exception
	 */
	public void delete(EvaluatorsModel model)throws Exception{
		//dao.delete(model.getIds());
		for(Long l:model.getIds()){
			dao.runHql("update Report set evaluators=null where evaluators.id="+l);
			dao.delete(l);
		}
	}
	/**
	 * 修改实体
	 * @param model
	 * @throws Exception
	 */
	public Evaluators editDo(Evaluators bean){
		Updater<Evaluators> updater = new Updater<Evaluators>(bean);
		Evaluators ret = dao.updateByUpdater(updater);
		return ret;
		
	}
	/**
	 * 刷新实体
	 * @param model
	 * @throws Exception
	 */
	public void refresh(EvaluatorsModel model)throws Exception{
		
	}
	
	/**
	 * 浏览详情
	 * @param model
	 * @throws Exception
	 */
	public void view(EvaluatorsModel model)throws Exception{
		Evaluators entity = dao.get(model.getId());
		Assert.notNull(entity, "你查看的对象不存在");
		
		model.setBean(entity);
	}
	
	/**
	 * 分页浏览
	 * @param model
	 * @throws Exception
	 */
	public void list(EvaluatorsModel model)throws Exception{
		StringBuilder sb = new StringBuilder("FROM Evaluators where 1=1");	
		if(StringUtils.hasText(model.getKeyword())){
			sb.append(" and name like '%"+model.getKeyword()+"%'");
		}
		
		Finder f = Finder.create(sb.toString());
		f.append(" order by id desc");
		
		dao.find(f, model);
	}
	
	/**
	 * 判断实体是否存在
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public boolean isExist(EvaluatorsModel model)throws Exception{
		return false;
	}
	
	@Override
	public Evaluators get(Long id) {
		return dao.get(id);
	}
	
	@Override
	public Page findForSelect(Page p, String keyword) {
		List<Evaluators> list = dao.findForSelect(p.getTotalCount(), p.getPageSize(), keyword);
		p.setList(list);
		
		return p;
	}
}